package net.cruciblesoftware.ghostspacekillah;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.Rect;
import java.util.Random;

class EnemyShip extends Ship {

    private int maxVel;
    private float xScale, yScale;

    public EnemyShip() {
        super(FieldActor.TYPE_ENEMY);
        posX = rand.nextInt(SceneView.width - imgWidth) + (imgWidth / 2);
        posY = 0;
        maxVel = (int)(SceneView.height * 
                       Settings.getPercent("enemy_max_vel"));
        rotation = rand.nextInt(180) + 180;
        xScale = (float)Math.cos(Math.toRadians(rotation));
        yScale = (float)Math.sin(Math.toRadians(rotation));
        yScale = Math.abs(yScale);
    }


    @Override
    public void update(float elapsed) {
        super.update(elapsed);

        // update positions
        posX += maxVel * xScale * elapsed;
        posY += maxVel * yScale * elapsed;

        // check Y bounds, allow ship to get off screen
        if(posY >= (SceneView.height + (imgHeight / 2))) {
            setState(Ship.STATE_DEAD);
            return;
        }

        // check X bounds
        if(posX < 0.0f || posX > SceneView.width) {
            if(posX < 0.0f)
                rotation = rand.nextInt(75) + 270;
            else
                rotation = rand.nextInt(75) + 195;
            xScale = (float)Math.cos(Math.toRadians(rotation));
            yScale = (float)Math.sin(Math.toRadians(rotation));
            yScale = Math.abs(yScale);
        }
    }


    @Override
    public void draw(Canvas c) {
        int w_2 = imgWidth / 2;
        int h_2 = imgHeight / 2;
        src.right = imgWidth;
        src.bottom = imgHeight;
        dest.left = (int)(posX - w_2);
        dest.top = (int)(posY - h_2);
        dest.right = (int)(posX + w_2);
        dest.bottom = (int)(posY + h_2);
        c.save();
        c.rotate(-1.0f * (float)(rotation - 90), posX, posY);
        c.drawBitmap(img, src, dest, null);
        c.restore();
    }

    
    /* Logging functions */
    private void logi(String msg) {
        android.util.Log.i(this.getClass().getName(), msg);
    }
    private void logw(String msg) {
        android.util.Log.w(this.getClass().getName(), msg);
    }
    private void loge(String msg) {
        android.util.Log.e(this.getClass().getName(), msg);
    }
}
